#include "ImgLoader.h"

ImgLoader::ImgLoader() 
{
}

ImgLoader::~ImgLoader() 
{
}

ImgLoader* ImgLoader::m_xInstance = nullptr;

ImgLoader* ImgLoader::GetInstance()
{
	if (m_xInstance == nullptr)
	{
		m_xInstance = new ImgLoader();
	}
	return m_xInstance;
}

void ImgLoader::Destroy()
{
	if (m_xInstance != NULL)
	{
		delete m_xInstance;
		m_xInstance = NULL;
	}
}

sf::Texture* ImgLoader::loadImage(std::string p_Path, int p_DrawPriority, bool p_Persistent)
{
	for (unsigned int i = 0; i < m_xImages.size(); i++)
	{
		if (m_xImages[i].m_sPath == p_Path)
		{
			return m_xImages[i].m_xTexture;
		}
	}

	Image image; //  = new Image;*/
	sf::Texture* texture = new sf::Texture();
	texture->setSmooth(true);
	image.m_xTexture = texture;
	image.m_sPath = p_Path;
	image.m_bPersistent = p_Persistent;
	image.m_xTexture->loadFromFile(image.m_sPath);
	m_xImages.push_back(image);

	return image.m_xTexture;
}

void ImgLoader::ClearImages()
{
	for (unsigned int i = 0; i < m_xImages.size(); i++)
	{
		if (m_xImages[i].m_bPersistent == false)
		{
			delete m_xImages[i].m_xTexture;
			m_xImages[i].m_xTexture = nullptr;
			m_xImages.erase(m_xImages.begin() + i);
		}
	}
}

void ImgLoader::Cleanup()
{
	for (unsigned int i = 0; i < m_xImages.size(); i++)
	{
		delete m_xImages[i].m_xTexture;
		m_xImages[i].m_xTexture = nullptr;

	}
	m_xImages.clear();
}
